package com.admin.education.controllers.business;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.schools.SchoolsTransfer;
import com.admin.education.dataTransferObject.schools.request.CreateSchoolRequest;
import com.admin.education.dataTransferObject.schools.request.QuerySchoolRequest;
import com.admin.education.dataTransferObject.schools.request.UpdateSchoolRequest;
import com.admin.education.services.Interfaces.ISchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;

@JwtAuthorize
@RestController(value = "adminSchoolsController")
@RequestMapping("schools")
@CrossOrigin(origins = "*", maxAge = 3600)
public class SchoolsController {
    @Autowired
    private ISchoolService schoolService;

    @GetMapping("")
    public Callable<IResponseMessage> querySchools(@Valid QuerySchoolRequest querySchoolRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<PageResponse<SchoolsTransfer>> servicesExcuteResult = schoolService.querySchools(querySchoolRequest);

                if (servicesExcuteResult.isSuccess()) {
                    responseMessage = new ResponseMessageWithData<PageResponse<SchoolsTransfer>>(Result.Success, servicesExcuteResult.getData());
                } else {
                    throw new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @GetMapping("{schoolsId}")
    public Callable<IResponseMessage> querySchoolsById(@PathVariable Long schoolsId) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<SchoolsTransfer> result = schoolService.querySchoolById(schoolsId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessageWithData<SchoolsTransfer>(Result.Success, result.getData());
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @PostMapping("")
    public Callable<IResponseMessage> createSchools(@RequestBody CreateSchoolRequest createSchoolRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = schoolService.createSchools(createSchoolRequest);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @DeleteMapping("{schoolsId}")
    public Callable<IResponseMessage> deleteSchoolsById(@PathVariable Long schoolsId) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = schoolService.deleteSchool(schoolsId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @PutMapping("{schoolsId}")
    public Callable<IResponseMessage> updateSchools(@PathVariable Long schoolsId, @RequestBody @Valid UpdateSchoolRequest updateSchoolRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = schoolService.updateSchools(schoolsId, updateSchoolRequest);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @PutMapping("{schoolsId}/Valid")
    public Callable<IResponseMessage> updateSchoolsValid(@PathVariable Long schoolsId) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = schoolService.updateSchoolsValid(schoolsId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
}
